package com.eye.catcher.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

public class DownloadUtils {



    public static void downloadFile(HttpServletRequest request, HttpServletResponse response,
                String filePath,String fileName)
            throws IOException {

        // 获取浏览器的信息
        String agent = request.getHeader("USER-AGENT");
        if (agent != null && agent.toLowerCase().indexOf("FIRE_FOX") > 0) {
            //火狐浏览器自己会对URL进行一次URL转码所以区别处理
            response.setHeader("Content-Disposition",
                    "attachment; filename=" + new String(fileName.getBytes("GB2312"), "ISO-8859-1"));
        } else if (agent.toLowerCase().indexOf("SAFARI") > 0) {
            //苹果浏览器需要用ISO 而且文件名得用UTF-8
            response.setHeader("Content-Disposition",
                    "attachment; filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
        } else {
            //其他的浏览器
            response.setHeader("Content-Disposition",
                    "attachment; filename=\"" + java.net.URLEncoder.encode(fileName, "UTF-8"));
        }

        File file = new File(filePath);
        FileInputStream fileInputStream = new FileInputStream(file);
        OutputStream os = response.getOutputStream();
        byte[] buff = new byte[1024];
        int len = -1;
        while ((len = fileInputStream.read(buff)) > 0) {
            os.write(buff, 0, len);
        }
        os.flush();
        os.close();
        fileInputStream.close();

    }

    public static void downloadFile(HttpServletRequest request, HttpServletResponse response,
                                    InputStream inputStream,String fileName){
        OutputStream os = null;
        try{
            // 获取浏览器的信息
            String agent = request.getHeader("USER-AGENT");
            if (agent != null && agent.toLowerCase().indexOf("FIRE_FOX") > 0) {
                //火狐浏览器自己会对URL进行一次URL转码所以区别处理
                response.setHeader("Content-Disposition",
                        "attachment; filename=" + new String(fileName.getBytes("GB2312"), "ISO-8859-1"));
            } else if (agent.toLowerCase().indexOf("SAFARI") > 0) {
                //苹果浏览器需要用ISO 而且文件名得用UTF-8
                response.setHeader("Content-Disposition",
                        "attachment; filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
            } else {
                //其他的浏览器
                response.setHeader("Content-Disposition",
                        "attachment; filename=\"" + java.net.URLEncoder.encode(fileName, "UTF-8"));
            }
//        InputStream fis = new BufferedInputStream(inputStream);
            os = response.getOutputStream();

            byte[] buff = new byte[1024];
            int len = -1;
            while ((len = inputStream.read(buff)) > 0) {
                os.write(buff, 0, len);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                os.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



    }
    public static void downloadFileForView(HttpServletRequest request, HttpServletResponse response,
                                    InputStream inputStream,String fileName)
            throws IOException {

        // 获取浏览器的信息
        String agent = request.getHeader("USER-AGENT");
        if (agent != null && agent.toLowerCase().indexOf("FIRE_FOX") > 0) {
            //火狐浏览器自己会对URL进行一次URL转码所以区别处理
            response.setHeader("Content-Disposition",
                    "inline; filename=" + new String(fileName.getBytes("GB2312"), "ISO-8859-1"));
        } else if (agent.toLowerCase().indexOf("SAFARI") > 0) {
            //苹果浏览器需要用ISO 而且文件名得用UTF-8
            response.setHeader("Content-Disposition",
                    "inline; filename=" + new String(fileName.getBytes("UTF-8"), "ISO-8859-1"));
        } else {
            //其他的浏览器
            response.setHeader("Content-Disposition",
                    "inline; filename=\"" + java.net.URLEncoder.encode(fileName, "UTF-8"));
        }

        OutputStream os = response.getOutputStream();

        byte[] buff = new byte[1024];
        int len = -1;
        while ((len = inputStream.read(buff)) > 0) {
            os.write(buff, 0, len);
        }
        os.flush();
        os.close();

        inputStream.close();

    }

}
